<script setup lang="ts">
import { getCurrentInstance, ref, reactive, onMounted } from 'vue'
import { onBeforeRouteLeave } from 'vue-router'
const { proxy } = getCurrentInstance()

/* ---- */
const userInfo = proxy.$Store.get('userInfo') || {}
let logUser = proxy.$Store.get('logUser') || {}
const WXConfig = proxy.$Store.get('WXConfig') || {}
const forFees = proxy.$Fees || []

/* --设置微信分享功能-- */
proxy.$WxTool.loadJsdk(WXConfig, (obj) => {
  if (!obj) return
  // ----
  obj.hideMenuItem()
})

/* ---- */
const user = reactive({
  cloudWork: false,
  // ----
  cloudPayId: 0,
  cloudFeeId: 'cloudRent',
  cloudFeeVal: 0,
  cloudFeeNum: 0,
  cloudFeeUnit: '',
  cloudFeeUse: true,
  cloudTime: '',
  // ----
  forAdm: String(proxy.$Phone) === String(logUser.telphone)
})
const items = ref([])

/* ---- */
const layer = ref()
const layerBox = ref()

/* ---- */
const setPublicInfo = async (item, callback) => {
  await proxy.$http[item.type](item.url, item.data).then((res) => {
    if (res) callback && callback(res)
  }).catch((err) => { proxy.$Myfun.forToast(layer.value, 'open', err) })
}
const setPayState = async () => {
  if (proxy.$route.query.forToast) proxy.$Myfun.forToast(layer.value, 'open', proxy.$route.query.forToast)
  // ----
  if (proxy.$route.query.forAttr && proxy.$route.query.forVal) {
    const forData = {
      config_id: userInfo.config_id,
      openid: userInfo.openid
    }
    const attrArr = proxy.$route.query.forAttr.split(',')
    const valArr = proxy.$route.query.forVal.split(',')
    attrArr.forEach((item, index) => {
      forData[item] = Number(valArr[index])
    })
    await setPublicInfo({
      type: 'patch',
      url: 'dealer/update?id=' + logUser.id,
      data: forData
    }, (res) => {
      logUser = res
      // --清空参数--
      proxy.$Myfun.openPage(proxy, 'replace', proxy.$route.name)
    })
  }
}
// ----
const getCurrencyInfo = async () => {
  await setPayState()
  // ----
  forFees.some((item) => {
    if (String(item.id) === String(user.cloudFeeId)) {
      user.cloudFeeVal = Number(item.amount)
      user.cloudFeeNum = Number(item.num)
      user.cloudFeeUnit = item.typeText
      user.cloudFeeUse = item.isOpenUse
    }
  })
  // --获取充值有效期--
  if (user.cloudFeeUse) {
    let workLine = []
    let amountDateRetail = 0
    proxy.$Myfun.getJsonData(logUser.topupze, (resJson) => { workLine = resJson })
    workLine.some((oneItem) => {
      if (String(oneItem.id) === String(user.cloudFeeId)) {
        oneItem.info.some((twoItem) => {
          if (String(twoItem.id) === String(user.cloudPayId)) {
            user.cloudTime = twoItem.endtime.split(' ')[0]
            amountDateRetail = Date.parse(new Date(twoItem.endtime.replace(/-/g, '/'))) || 0
          }
        })
      }
    })
    const curDate = Date.parse(new Date())
    user.cloudWork = Number(amountDateRetail) >= Number(curDate)
  }
  // ----
  items.value = [{
    show: true,
    child: [{
      show: true,
      isArrow: true,
      name: '门店名称',
      callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'StoreWords') }
    }, {
      show: true,
      isArrow: true,
      name: '门店标牌',
      callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'StoreAvatar') }
    }, {
      show: true,
      isArrow: true,
      name: '在线客服',
      callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'WechatAdm') }
    }, {
      show: true,
      isArrow: true,
      name: '通用信息',
      callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'InfoAdm', { name: '通用信息' }) }
    }]
  }, {
    show: true,
    child: [{
      show: true,
      isArrow: true,
      name: '计量单位',
      callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'CloudUnit') }
    }, {
      show: true,
      isArrow: true,
      name: '商品分类',
      callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'CloudCate') }
    }, {
      show: true,
      isArrow: true,
      name: '上传商品',
      callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'CloudPdtAdm') }
    }, {
      show: true,
      isArrow: true,
      name: '管理商品',
      callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'CloudPdtList') }
    }, {
      show: true,
      isArrow: true,
      name: '预览分享',
      callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'CloudHome', { dealer_id: logUser.id }) }
    }]
  }, {
    show: !user.forAdm,
    child: [{
      show: true,
      isBtn: true,
      isOpen: Number(logUser.is_cloud) === 1,
      msg: user.cloudWork && user.cloudTime && user.cloudFeeUse ? '有效期至：' + user.cloudTime : '',
      name: '开仓分销',
      callback: (item) => {
        const obj = event.target
        if (String(obj.tagName) === 'LABEL') {
          if (!user.cloudWork && user.cloudFeeUse) {
            const options = {
              type: 6,
              maxCount: 5,
              title: '入驻【万域商友会】云仓分销',
              content: '费用：' + user.cloudFeeVal.toFixed(2) + '元/' + user.cloudFeeNum + user.cloudFeeUnit,
              button: [{
                text: proxy.$Global.layer.btnCancel
              }, {
                text: '我要分销',
                class: 'xw-color-24',
                callback: (count) => {
                  layerBox.value.close()
                  // ----
                  const forToPay = { payType: user.cloudFeeId, regType: 'jobs', payId: user.cloudPayId, payIdKey: 'jobs_id', payCount: count }
                  const forExtend = { name: proxy.$route.name, opts: { forToast: '入驻【万域云仓分销】付费成功', forAttr: 'is_cloud', forVal: 1 } }
                  proxy.$Myfun.openPage(proxy, 'push', 'PaySys', { toPay: JSON.stringify(forToPay), exTend: JSON.stringify(forExtend) })
                }
              }],
              callback: (count) => {
                layerBox.value.layer.content = '费用：' + Number(user.cloudFeeVal * count).toFixed(2) + '元/' + Number(user.cloudFeeNum * count) + user.cloudFeeUnit
              }
            }
            proxy.$Myfun.isIndepth(layerBox.value, 'open', options)
          } else {
            let curField = 1
            if (item.isOpen) curField = 0
            // ----
            setPublicInfo({
              type: 'patch',
              url: 'dealer/update?id=' + logUser.id,
              data: {
                is_cloud: curField,
                // ----
                config_id: userInfo.config_id,
                openid: userInfo.openid
              }
            }, (res) => { item.isOpen = !item.isOpen })
          }
        }
      }
    }, {
      show: true,
      isArrow: true,
      name: '分销商友',
      callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'CloudFriend', { dealer_id: logUser.id }) }
    }]
  }]
}
const getAuditFun = (type, item) => {
  switch (Number(type)) {
    case 1: {
      item.callback && item.callback(item)
      break
    }
  }
}

/* ---- */
onMounted(() => {
  getCurrencyInfo()
})
onBeforeRouteLeave(() => { proxy.$Myfun.setScrollTop(proxy.$refs.formSection) })
</script>

<template>
  <div class="mescroll-touch" ref="formSection">
    <div class="xw-msg xw-bgCL-11"><span>电脑端管理云仓更便捷：pc.wanyu110.com</span></div>
    <ul class="userSys xw-flex xw-flex-column">
      <template v-for="(item, index) in items">
      <li v-if="item.show">
        <template v-for="(son, num) in item.child">
        <a class="xw-Tfg xw-flex xw-flex-align" @click="getAuditFun(1, son)" v-if="son.show">
          <div class="USS-title xw-flex-W1">{{son.name}}</div>
          <span v-if="son.msg" v-html="son.msg"></span>
          <div class="USS-check" v-if="son.isBtn"><input type="checkbox" class="a-switch" v-model="son.isOpen"/><label></label></div>
          <i class="fal fa-angle-right" v-if="son.isArrow"></i>
        </a>
        </template>
      </li>
      </template>
    </ul>
    <layer-view ref="layer"/>
    <layer-view ref="layerBox"/>
  </div>
</template>

<style scoped>
/* ---- */
.userSys li:nth-of-type(1) { margin: 1rem 0; }
</style>